<template>
  <div>
    <h1 style="text-align: center;">用户列表</h1>
    <router-link to="/user/add">
      <el-button type="primary">用户添加</el-button>
    </router-link>
    
    <el-table
        :data="userlist"
        style="width: 100%"
        :row-class-name="tableRowClassName">
        <el-table-column
          prop="username"
          label="用户名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          width="180">
        </el-table-column>
        <el-table-column
          prop="tel"
          label="手机号">
        </el-table-column>
        <el-table-column
          label="角色">
          <template slot-scope="scope">
            <span>{{scope.row.role ? scope.row.role.name : '前台用户'}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="启用">
          <template slot-scope="scope">
            <el-switch
            v-model="scope.row.enable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="onChange(scope.row._id)"
            >
          </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="注册时间">
          <template slot-scope="scope">
            {{scope.row.createdAt | time}}
          </template>
        </el-table-column>
        <el-table-column
          label="修改时间">
          <template slot-scope="scope">
            {{scope.row.updatedAt | time}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 编辑用户的dialog -->
      <el-dialog
        title="用户编辑"
        :visible.sync="dialogVisible"
        width="30%"
        >
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="用户名">
                <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input type="password" v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
                <el-input v-model="form.tel"></el-input>
            </el-form-item>
            <el-form-item label="角色">
                <el-select v-model="form.role" placeholder="请选择角色">
                    <el-option v-for="role in roles" :key="role._id" :label="role.name" :value="role._id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否启用">
                <el-switch
                v-model="form.enable"
                active-color="#13ce66"
                inactive-color="#ff4949">
                </el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">立即创建</el-button>
                <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
  </div>
  
</template>

<script>
import {userList, changeEnable, roleList, userEdit, userDel} from '@/request'
import moment from 'moment'
export default {  
  filters: {
    time: (data) => {
      return moment(data).format('YYYY-MM-DD HH:mm:ss')
    }
  },
    data () {
        return {
            userlist: [],
            dialogVisible: false,
            form: {
                username: '',
                password: '',
                email: '',
                tel: '',
                enable: false,
                role: ''
            },
            roles: []
        }
    },
    methods: {
      // 修改是否启用
      onChange(id) {
        changeEnable(id).then(data => {
          // console.log(data);
          let {msg, error_code} = data
          if(error_code === 200) {
            this.$message({
              type: 'success',
              duration: 1500,
              message: msg
            })
          } else {
            this.$message({
              type: 'error',
              duration: 1500,
              message: msg
            })
          }
        })
      },
       getUserList() {
        userList().then(data => {
          // console.log(data);
          this.userlist = data.data
        })
       },
       tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row';
        } else if (rowIndex === 3) {
          return 'success-row';
        }
        return '';
      },
      handleEdit(index, row) {
        this.dialogVisible = true
        // console.log(index, row);
        row.password = ''
        this.form = row
      },
      handleDelete(index, row) {
        // console.log(index, row);
        this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 发请求删除
          userDel(row._id).then(data => {
            let {msg, error_code} = data
            if(error_code === 200) {
              this.userlist.splice(index, 1)
              this.$message({
                type: 'success',
                message: msg
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      getRoles() {
          roleList().then(data => {
              // console.log(data);
              this.roles = data.data.rolelist
          })
      },
      onSubmit() {
        // console.log(this.form);
        userEdit(this.form).then(data => {
          // console.log(data);
          let {error_code, msg} = data
          if(error_code === 200) {
              this.$message({
                  message: msg,
                  duration: 1500,
                  type: 'success',
                  onClose: () => {
                    this.dialogVisible = false
                    this.form = {
                      username: '',
                      password: '',
                      email: '',
                      tel: '',
                      enable: false,
                      role: ''
                    }
                  }
              })
          } else {
              this.$message({
                  message: msg,
                  duration: 1500,
                  type: 'error'
              })
          }
        })
      }
    },
    created() {
        this.getRoles()
        this.getUserList()
    }
}
</script>

<style lang="scss" scoped>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
